import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:management/app/cons.dart';
import 'package:management/model/department_model.dart';
import 'package:management/model/visit_matter_model.dart';
import 'package:management/utils/convert.dart';
import 'package:management/model/visitor_model.dart';
import 'package:management/utils/log_utils.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 来访事由
@deprecated
class VisitReason extends StatefulWidget {
  final Visitor? visitor;

  const VisitReason({Key? key,  this.visitor}) : super(key: key);

  @override
  _VisitReasonState createState() => _VisitReasonState();
}

class _VisitReasonState extends State<VisitReason> {
  late TextEditingController _textEditingControllerInterviewee;
  late TextEditingController _textEditingControllerRemark;
  late TextEditingController _textEditingControllerWhatToBring;
  late TextEditingController _textEditingControllerLicensePlateNumber;
  late TextEditingController _textEditingControllerNumberOfAccompanyingVisitors;
  late TextEditingController _textEditingControllerRegistrationPoint;
  late TextEditingController _textEditingControllerDate;
  late TextEditingController _textEditingControllerDateTime;
  String _department = "行政部";

  String _matter = "面试";

  String _currentDate = Convert.currentDate();
  String _currentDateTime = Convert.currentDateTime(second: false);
  late Visitor visitor;
  List<Department>? departments = [];
  List<VisitMatter>? matters = [];

  @override
  void initState() {
    SystemChrome.setEnabledSystemUIOverlays([]);
    _textEditingControllerInterviewee = TextEditingController();
    _textEditingControllerRemark = TextEditingController();
    _textEditingControllerWhatToBring = TextEditingController();
    _textEditingControllerLicensePlateNumber = TextEditingController();
    _textEditingControllerNumberOfAccompanyingVisitors =
        TextEditingController();
    _textEditingControllerRegistrationPoint = TextEditingController();
    _textEditingControllerRegistrationPoint.text = SpUtil.getString(
        Strings.sp_key_registrationPoint,
        defValue: Strings.sp_defaultValue_registrationPoint)!;
    _textEditingControllerDate = TextEditingController();
    _textEditingControllerDateTime = TextEditingController();
    queryDepartmentAndVisitMatter();

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // visitor = ModalRoute.of(context).settings.arguments;
    visitor = widget.visitor!;

    return Scaffold(
      appBar: _buildAppBar(),
      body: _buildBody(),
    );
  }

  Widget _buildBody() => ListView(
    children: [
      Container(
        width: 1.sw,
        height: 1.sh,
        color: Colors.black12,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Container(
              padding: EdgeInsets.all(20.w),
              width: 1.sw,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "被   访  人",
                        style: TextStyle(
                          fontSize: 25.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                  Container(
                    width: 0.9.sw,
                    height: 0.1.sh,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12.w),
                        border: Border.all(color: Colors.grey, width: 2.w),
                        color: Colors.white),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Container(
                          child: Row(
                            mainAxisAlignment:
                            MainAxisAlignment.spaceAround,
                            children: [
                              Text(
                                "被访部门",
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 25.sp,
                                ),
                              ),
                              Container(
                                padding: EdgeInsets.only(left: 10.w),
                                height: 50.h,
                                width: 700.w,
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.grey),
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(5.w),
                                    topRight: Radius.circular(5.w),
                                    bottomLeft: Radius.circular(5.w),
                                    bottomRight: Radius.circular(5.w),
                                  ),
                                ),
                                child: DropdownButtonHideUnderline(
                                  child: DropdownButton(
                                    hint: Text(
                                      "请选择被访部门",
                                      style: TextStyle(fontSize: 20),
                                    ),
                                    value: _department,
                                    items: departments!
                                        .map((e) => DropdownMenuItem(
                                        value: e.name,
                                        child: Text(
                                          e.name.toString(),
                                          style: TextStyle(
                                              fontSize: 20,
                                              fontWeight:
                                              FontWeight.bold),
                                        )))
                                        .toList(),
                                    onChanged: (value) {
                                      MyLog.d("被访部门：$value");
                                      setState(() => _department = value.toString());
                                    },
                                  ),
                                ),
                              ),
                              Text(
                                "被  访  人",
                                style: TextStyle(
                                    fontWeight: FontWeight.bold,
                                    fontSize: 25),
                              ),
                              Container(
                                width: 700.w,
                                height: 50.h,
                                child: TextField(
                                  controller:
                                          _textEditingControllerInterviewee,
                                      style: TextStyle(
                                        fontSize: 20,
                                      ),
                                      textAlign: TextAlign.left,
                                      textAlignVertical:
                                          TextAlignVertical.bottom,
                                      decoration: InputDecoration(
                                        hintText: "请输入被访人姓名",
                                        hintStyle: TextStyle(fontSize: 20),
                                        border: OutlineInputBorder(),
                                      ),
                                    ),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ), //被访人
            Container(
              width: MediaQuery.of(context).size.width,
              padding: EdgeInsets.only(left: 20, right: 20),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "访问事由",
                    style: TextStyle(
                        fontSize: 25, fontWeight: FontWeight.bold),
                  ),
                  Container(
                    width: 0.9.sw,
                    height: 0.1.sh,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12.w),
                        border: Border.all(color: Colors.grey, width: 2.w),
                        color: Colors.white),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Text(
                          "来访事项",
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 25.sp),
                        ),
                        Container(
                          padding: EdgeInsets.only(left: 10.w),
                          height: 50.h,
                          width: 700.w,
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(5.w),
                              topRight: Radius.circular(5.w),
                              bottomLeft: Radius.circular(5.w),
                              bottomRight: Radius.circular(5.w),
                            ),
                          ),
                          child: DropdownButtonHideUnderline(
                            child: DropdownButton(
                              hint: Text(
                                "请选择来访事项",
                                style: TextStyle(fontSize: 20),
                              ),
                              value: _matter,
                              items: matters!
                                  .map((e) => DropdownMenuItem(
                                  value: e.matter,
                                  child: Text(
                                    e.matter!,
                                    style: TextStyle(
                                        fontSize: 20.sp,
                                        fontWeight: FontWeight.bold),
                                  )))
                                  .toList(),
                              onChanged: (value) {
                                MyLog.d("来访事项：$value");
                                setState(() => _matter = value.toString());
                              },
                            ),
                          ),
                        ),
                        Text(
                          "备        注",
                          style: TextStyle(
                              fontWeight: FontWeight.bold, fontSize: 25),
                        ),
                        Container(
                          width: 700.w,
                          height: 50.h,
                          child: TextField(
                            style: TextStyle(fontSize: 18.sp),
                            textAlign: TextAlign.left,

                            // textAlignVertical: TextAlignVertical.bottom,
                            controller: _textEditingControllerRemark,
                            decoration: InputDecoration(
                              hintStyle: TextStyle(fontSize: 18),
                              border: OutlineInputBorder(),
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ), //访客事由
            Container(
              width: 1.sw,
              padding: EdgeInsets.only(left: 20, right: 20, top: 20),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    '登记情况',
                    style: TextStyle(
                        fontSize: 25.sp, fontWeight: FontWeight.bold),
                  ),
                  Container(
                    width: 0.9.sw,
                    height: 0.3.sh,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12.w),
                        border: Border.all(color: Colors.grey, width: 2.w),
                        color: Colors.white),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text("携带物品",
                                style: TextStyle(
                                    fontSize: 25,
                                    fontWeight: FontWeight.bold)),
                            Container(
                              width: 700.w,
                              height: 50.h,
                              child: TextField(
                                controller:
                                        _textEditingControllerWhatToBring,
                                    textAlignVertical: TextAlignVertical.bottom,
                                    style: TextStyle(fontSize: 25),
                                    decoration: InputDecoration(
                                        hintText: "请输入携带物品",
                                        hintStyle: TextStyle(fontSize: 20.sp),
                                        border: OutlineInputBorder()),
                                  ),
                            ),
                            Text("车牌号码",
                                style: TextStyle(
                                    fontSize: 25.sp,
                                    fontWeight: FontWeight.bold)),
                            Container(
                              width: 700.w,
                              height: 50.h,
                              child: TextField(
                                controller:
                                _textEditingControllerLicensePlateNumber,
                                style: TextStyle(fontSize: 18.sp),
                                textAlignVertical: TextAlignVertical.bottom,
                                decoration: InputDecoration(
                                    hintText: "请输入车牌号码",
                                    hintStyle: TextStyle(fontSize: 20.sp),
                                    border: OutlineInputBorder()),
                              ),
                            )
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text("随行访客",
                                style: TextStyle(
                                    fontSize: 25.sp,
                                    fontWeight: FontWeight.bold)),
                            Container(
                              width: 700.w,
                              height: 50.h,
                              child: TextField(
                                controller:
                                _textEditingControllerNumberOfAccompanyingVisitors,
                                style: TextStyle(fontSize: 18.sp),
                                textAlignVertical: TextAlignVertical.bottom,
                                decoration: InputDecoration(
                                    hintText: "请输入随行访客人数",
                                    hintStyle: TextStyle(fontSize: 20.sp),
                                    border: OutlineInputBorder()),
                              ),
                            ),
                            Text("登  记  点",
                                style: TextStyle(
                                    fontSize: 25.sp,
                                    fontWeight: FontWeight.bold)),
                            Container(
                              width: 700.w,
                              height: 50.h,
                              child: TextField(
                                controller:
                                _textEditingControllerRegistrationPoint,
                                style: TextStyle(fontSize: 18.sp),
                                textAlignVertical: TextAlignVertical.bottom,
                                decoration: InputDecoration(
                                    hintText: '请输入登记点',
                                    hintStyle: TextStyle(fontSize: 20.sp),
                                    border: OutlineInputBorder()),
                              ),
                            )
                          ],
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            Text("登记日期",
                                style: TextStyle(
                                    fontSize: 25.sp,
                                    fontWeight: FontWeight.bold)),
                            Container(
                              width: 700.w,
                              height: 50.h,
                              child: TextField(
                                    controller: _textEditingControllerDate,
                                    readOnly: false,
                                    textAlignVertical: TextAlignVertical.bottom,
                                    style: TextStyle(fontSize: 20.sp),
                                    decoration: InputDecoration(
                                        hintText: _currentDate,
                                        hintStyle: TextStyle(fontSize: 20.sp),
                                        border: OutlineInputBorder()),
                                  ),
                                ),
                                Text(
                                  "时        间",
                                  style: TextStyle(
                                      fontSize: 25.sp,
                                      fontWeight: FontWeight.bold),
                                ),
                                Container(
                                  width: 700.w,
                                  height: 50.h,
                                  child: TextField(
                                    controller: _textEditingControllerDateTime,
                                    readOnly: false,
                                    textAlignVertical: TextAlignVertical.bottom,
                                    style: TextStyle(fontSize: 20.sp),
                                    decoration: InputDecoration(
                                        hintText: _currentDateTime,
                                        hintStyle: TextStyle(fontSize: 20.sp),
                                        border: OutlineInputBorder()),
                                  ),
                            )
                          ],
                        )
                      ],
                    ),
                  )
                ],
              ),
            ), //登记情况
          ],
        ),
      ),
    ],
  );

  AppBar _buildAppBar() => AppBar(
    leading: IconButton(
      icon: Icon(Icons.arrow_back),
      iconSize: 35,
      onPressed: () => Navigator.of(context).pop(),
    ),
    title: Text("访问事由",
        style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.sp)),
    backgroundColor: Colors.blue,
    centerTitle: true,
    actions: [
      Container(
        width: 120.w,
        height: 30.h,
        alignment: Alignment.center,
        child: MaterialButton(
            child: Text(
              "完成",
              style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 25.sp),
            ),
            onPressed: () {
              MyLog.i("--- 完成 ----");
              onComplete();
              // VisitorProvider.provider.insert(visitor);
            }),
      ),
    ],
  );

  void onComplete() {
    visitor.departmentInterviewed = _department;
    visitor.interviewee = _textEditingControllerInterviewee.text;
    visitor.visitingMatters = _matter;
    visitor.remarks = _textEditingControllerRemark.text;
    visitor.whatToBring = _textEditingControllerWhatToBring.text;
    visitor.licensePlateNumber = _textEditingControllerLicensePlateNumber.text;
    visitor.numberOfAccompanyingVisitors =
        _textEditingControllerNumberOfAccompanyingVisitors.text;
    visitor.registrationPoint = _textEditingControllerRegistrationPoint.text;
    visitor.registrationDate = TextUtil.isEmpty(_textEditingControllerDate.text)
        ? _currentDate
        : _textEditingControllerDate.text;

    visitor.checkInTime = TextUtil.isEmpty(_textEditingControllerDateTime.text)
        ? _currentDateTime
        : _textEditingControllerDateTime.text;

    VisitorProvider.provider.insert(visitor);
    Fluttertoast.showToast(msg: "登记完成");
    Navigator.of(context)
        .pushNamedAndRemoveUntil('/HomePage', (route) => false);
  }

  void queryDepartmentAndVisitMatter() async {
    departments = await DepartmentProvider().queryAll();
    if (departments == null) {
      departments = [];
    }
    matters = await VisitMatterProvider().queryAll();
    if (matters == null) {
      matters = [];
    }
    setState(() {});
  }

}
